Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mismatch for Conway predicate failures #4666

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

aniketd
Copy link
Contributor

@aniketd aniketd commented Oct 7, 2024

Description

This is the second (Conway) part of #4619.

Checklist

  • Commit sequence broadly makes sense and commits have useful messages
  • New tests are added if needed and existing tests are updated
  • All visible changes are prepended to the latest section of a CHANGELOG.md for the affected packages.
    New section is never added with the code changes. (See RELEASING.md)
  • When applicable, versions are updated in .cabal and CHANGELOG.md files according to the
    versioning process.
  • The version bounds in .cabal files for all affected packages are updated.
    If you change the bounds in a cabal file, that package itself must have a version increase. (See RELEASING.md)
  • Code is formatted with fourmolu (use scripts/fourmolize.sh)
  • Cabal files are formatted (use scripts/cabal-format.sh)
  • hie.yaml has been updated (use scripts/gen-hie.sh)
  • Self-reviewed the diff

@aniketd aniketd changed the title Aniketd/mismatch for conway Mismatch for Conway predicate failures Oct 7, 2024
@aniketd aniketd force-pushed the aniketd/mismatch-type-for-predicate-failures branch from 87dba8f to a6572dd Compare October 8, 2024 10:13
Base automatically changed from aniketd/mismatch-type-for-predicate-failures to master October 8, 2024 12:26
@aniketd aniketd force-pushed the aniketd/mismatch-for-conway branch 3 times, most recently from 7e89bac to 99cee0e Compare October 10, 2024 19:53
@aniketd aniketd marked this pull request as ready for review October 10, 2024 19:53
@aniketd aniketd requested a review from a team as a code owner October 10, 2024 19:53
Copy link
Contributor

@neilmayhew neilmayhew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice improvement! Your handling of the protocol encoding variations is neat, too.

My suggestions are mostly cosmetic.

However, there's a more substantial one at the end. It doesn't affect the functionality, but it does affect the readability.

I've also suggested a few additional places where Mismatch could be used.

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ledger.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxo.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxo.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxo.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxo.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxow.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxow.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxow.hs Outdated Show resolved Hide resolved
Copy link
Collaborator

@lehins lehins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All my comments in BBODY rule module apply to all other rules affected by this PR.

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Bbody.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Bbody.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxow.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxow.hs Outdated Show resolved Hide resolved
Copy link
Contributor

@TimSheard TimSheard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I only commented on the Coders parts.
The extra constraint should not be there. We should try hrd to get rid of it.

Copy link
Collaborator

@lehins lehins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great!
A few more simplifications and improvements and it will ready to go

Comment on lines 742 to 748
swapMismatch :: Mismatch r a -> Mismatch r a
swapMismatch m@(Mismatch _ _) =
let Mismatch {..} = m
in Mismatch
{ mismatchSupplied = mismatchExpected
, mismatchExpected = mismatchSupplied
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just a terrible hack. Why would you do something like this: mismatchSupplied = mismatchExpected?

If you have a type that has some invariant or just a semantic meaning, you should try to preserve it at all times, regardless of the fact that its use could be ephemeral. In other words if something is "expected" it can't all of a sudden become "supplied"

Just use a tuple instead of this hack. Also we can never add any new fields to Mismatch, so there is no need to use this trick with pattern matching

Suggested change
swapMismatch :: Mismatch r a -> Mismatch r a
swapMismatch m@(Mismatch _ _) =
let Mismatch {..} = m
in Mismatch
{ mismatchSupplied = mismatchExpected
, mismatchExpected = mismatchSupplied
}
-- | Convert a `Mismatch` to a tuple that has "supplied" and "expected" swapped places
swapMismatch :: Mismatch r a -> (a, a)
swapMismatch Mismatch {mismatchSupplied, mismatchExpected} =
(mismatchExpected, mismatchSupplied)
-- | Convert a tuple that has "supplied" and "expected" swapped places to a `Mismatch` type.
unswapMismatch :: (a, a) -> Mismatch r a
unswapMismatch (mismatchExpected, mismatchSupplied) =
Mismatch {mismatchSupplied, mismatchExpected}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This makes it easier to understand what's going on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree 💯

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried this with specifying {Enc,Dec}CBORGroup instance for (a, a) and the types end up disagreeing because the Coders require the element to be of type Mismatch. Let me know if you have an idea around it. <$> and MapE also won't work.

libs/cardano-ledger-core/src/Cardano/Ledger/BaseTypes.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxow.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxow.hs Outdated Show resolved Hide resolved
eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Utxow.hs Outdated Show resolved Hide resolved
@aniketd aniketd force-pushed the aniketd/mismatch-for-conway branch 3 times, most recently from 34b1182 to e8ec767 Compare October 18, 2024 12:05
Also, add {Enc,Dec}CBORGroup instances for Mismatch
Rules:
- BBODY
- GOV
- GOVCERT
- LEDGER
- UTXO
- UTXOW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants